package com.apes.fn.scm.retailOrder.service.tc;


import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.DateUtil;
import com.apes.framework.util.MapUtil;
import com.apes.pi.model.Timestamp;
import com.apes.pi.repository.TimestampRepository;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.apes.framework.util.DateUtil.add;
import static com.apes.framework.util.DateUtil.format;

/**
 * 功能：跨公司会员余额结算
 *
 * @author linwz
 * @create 2020-12-22 16:56
 */
@Service("vipSurplusService")
public class vipSurplusService extends DomainService {

    @Autowired
    private TimestampRepository timestampRepository;

    /**
     * 定时任务,定时获取老框架会员余额结算
     */
    public void oldVipSurplusSk() throws ParseException {
        Date today = new Date();
        Timestamp timestamp = timestampRepository.findOne("vip.vipSurplusService.oldVipSurplusSk");
        String begin = format(DateUtil.parse(timestamp.getTime(), DateUtil.SHOW_DATETIME_FORMAT), DateUtil.SHOW_DATE_FORMAT);
        String end = format(add(today,5,1), DateUtil.SHOW_DATE_FORMAT);
        this.invoke("vip.vipSurplusSettleService.execOldVipSurplus", MapUtil.mapper(
                "DATE_START", com.apestech.framework.util.DateUtil.strToDate(begin),
                "DATE_END", com.apestech.framework.util.DateUtil.strToDate(end)));
        timestamp.setTime(format(today, DateUtil.SHOW_DATETIME_FORMAT));
        timestampRepository.saveAndFlush(timestamp);
    }


    /**
     * 传入的时间加一天
     * @param date
     * @return
     */
    private Date dateAdd(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +1);//+1今天的时间加一天
        return calendar.getTime();
    }


    /**
     * 提成日报 日结
     * @param request
     */
    public void VIPSURPLUSRBB(SimpleRequest request) {
        Date date = new Date();
        Date date1 = add(date,5,1);
        String today = format(date, com.apes.framework.util.DateUtil.SHOW_DATE_FORMAT);
        String tomorrow = format(date1, com.apes.framework.util.DateUtil.SHOW_DATE_FORMAT);
        this.invoke("condition.execReport", MapUtil.mapper(
                "TJRQ_STAR", today,
                "TJRQ_END",tomorrow,
                "reportId","VIP_SURPLUS_RBB"));

    }

}
